﻿using System;
using System.Collections.Generic;
using System.Text;

class BinaryRepresentationOfShort
{
    static void Main()
    {
        //Exercise 8.
        //Write a program that shows the binary representation of given 16-bit signed integer number (the C# type short).

        //For exampe: 254 -> 11111110;  500 -> 000111110100

        Console.Write("Enter a short number (from -32 768  to 32 767): ");
        int shortNumber = int.Parse(Console.ReadLine());

        List<int> binaryNumber = new List<int>();
        StringBuilder binary = new StringBuilder();

        if (shortNumber > 0)
        {
            while (shortNumber > 0)
            {
                binaryNumber.Add(shortNumber % 2);
                shortNumber = shortNumber / 2;
            }
            while (binaryNumber.Count % 16 != 0)
            {
                binaryNumber.Add(0);
            }
            binaryNumber.Reverse();

            for (int i = 0; i < binaryNumber.Count; i++)
            {
                binary.Append(binaryNumber[i]);
            }

            Console.WriteLine(binary);
        }
        else
        {
                
            shortNumber = Math.Abs(shortNumber) - 1;
            while(shortNumber>0)
            {
                binaryNumber.Add(shortNumber % 2);
                shortNumber = shortNumber / 2;
            }
            while (binaryNumber.Count % 16 != 0)
            {
                binaryNumber.Add(0);
            }
            binaryNumber.Reverse();
            for (int i = 0; i < binaryNumber.Count; i++)
            {
                if (binaryNumber[i] == 0)
                {
                    binary.Append(1);
                }
                else
                {
                    binary.Append(0);
                }
            }
            Console.WriteLine(binary); 
            
        }
    }
}

